package dome1;
import dome1.queuedemo.MyQueue;
import dome1.stackdome.MyStack;

import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class Text {
    //队列
    public static void main(String[] args) {
        MyQueue myQueue = new MyQueue();
        myQueue.offer(1);
        myQueue.offer(2);
        myQueue.offer(3);
        myQueue.offer(4);
        System.out.println(myQueue.poll());
        System.out.println(myQueue.peek());

        Queue<Integer> queue = new LinkedList<>();
        Deque<Integer> queue2 = new LinkedList<>();
    }
    //栈
    public static void main2(String[] args) {
        MyStack stack = new MyStack();
        stack.push(12);
        stack.push(23);
        stack.push(34);
        stack.push(45);

        int ret = stack.pop();//pop:获取栈顶的元素，并且删除
        System.out.println(ret);

        int ret2 = stack.peek();//peek:获取栈顶的元素，不删除
        System.out.println(ret2);

        System.out.println(stack.isEmpty());//empty:检查栈是否为空，返回boolean类型
    }

    //逆波兰表达式求值
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(int i = 0;i < tokens.length;i++){
            String str = tokens[i];
            if(!isOperations(str)){
                //不是运算符
                int val = Integer.valueOf(str);
                stack.push(val);
            }else{
                //是运算符
                int num2 = stack.pop();
                int num1 = stack.pop();

                switch(str){
                    case "+":
                        stack.push(num1+num2);
                        break;
                    case "-":
                        stack.push(num1-num2);
                        break;
                    case "*":
                        stack.push(num1*num2);
                        break;
                    case "/":
                        stack.push(num1/num2);
                        break;
                }
            }
        }
        return stack.pop();
    }
    private boolean isOperations(String str){
        if(str.equals("+") || str.equals("-") || str.equals("*") || str.equals("/")){
            return true;
        }
        return false;
    }

    //栈的压入、弹出序列
    //输入两个整数序列，第一个序列表示栈的压入顺序，请判断第二个序列是否可能为该栈的弹出顺序。
    public boolean IsPopOrder (int[] pushV, int[] popV) {
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for(int i = 0; i < pushV.length; i++){
            stack.push(pushV[i]);
            while(!stack.isEmpty()&&
                    j < popV.length &&
                    stack.peek() == popV[j]){
                stack.pop();
                j++;
            }
        }
        return j >= popV.length;
    }

    //有效的括号
    //给定一个只包括 '('，')'，'{'，'}'，'['，']' 的字符串 s ，判断字符串是否有效。
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for(int i = 0; i < s.length(); i++){
            char ch = s.charAt(i);
            if(ch == '(' || ch == '[' || ch == '{'){
                stack.push(ch);
            }else{
                //如果栈为空，不匹配
                if(stack.isEmpty()){
                    return false;
                }
                //栈不为空时，遇上不匹配的括号
                char ch2 = stack.peek();
                if(ch == ')' && ch2 == '(' || ch == ']' && ch2 == '[' || ch == '}' && ch2 == '{'){
                    stack.pop();
                }else {
                    return false;
                }
            }
        }
        //字符串遍历完，但是栈不为空，此时不匹配
        if(!stack.isEmpty()){
            return false;
        }
        return true;
    }

}
